package com.wwc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.business.controller.dto.train_seat.TrainSeatDeleteDTO;
import com.wwc.business.controller.dto.train_seat.TrainSeatInsertDTO;
import com.wwc.business.controller.dto.train_seat.TrainSeatSelectListDTO;
import com.wwc.business.controller.dto.train_seat.TrainSeatUpdateDTO;
import com.wwc.business.controller.vo.TrainSeatSelectListVO;
import com.wwc.business.entity.TrainCarriage;
import com.wwc.business.entity.TrainSeat;
import com.wwc.business.enums.SeatColEnum;
import com.wwc.business.mapper.TrainCarriageMapper;
import com.wwc.business.mapper.TrainSeatMapper;
import com.wwc.business.service.ITrainSeatService;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.response.PageResponse;
import com.wwc.common.utils.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TrainSeatServiceImpl extends ServiceImpl<TrainSeatMapper, TrainSeat> implements ITrainSeatService{
	@Resource
	private TrainSeatMapper trainSeatMapper;
	@Resource
	private TrainCarriageMapper trainCarriageMapper;

	@Override
	public PageResponse<TrainSeatSelectListVO> selectListPage(TrainSeatSelectListDTO trainSeatSelectListDTO){
		// 根据查询条件和分页配置查询座位分页列表
		Page<TrainSeat> page = new Page<>(trainSeatSelectListDTO.getPage(), trainSeatSelectListDTO.getSize());
		QueryWrapper<TrainSeat> trainSeatQueryWrapper = new QueryWrapper<>();
		trainSeatQueryWrapper.orderByAsc("train_code").orderByAsc("carriage_index").orderByAsc("`row`").orderByAsc("carriage_seat_index");
		if(StrUtil.isNotBlank(trainSeatSelectListDTO.getTrainCode())){
			trainSeatQueryWrapper.eq("train_code", trainSeatSelectListDTO.getTrainCode());
		}
		Page<TrainSeat> trainSeatPage = trainSeatMapper.selectPage(page, trainSeatQueryWrapper);

		// 返回自定义分页配置类
		PageResponse<TrainSeatSelectListVO> pageResponse = new PageResponse<>();
		pageResponse.setTotal(trainSeatPage.getTotal());
		pageResponse.setList(BeanUtil.copyToList(trainSeatPage.getRecords(), TrainSeatSelectListVO.class));
		return pageResponse;
	}

	@Override
	public void delete(TrainSeatDeleteDTO trainSeatDeleteDTO){
		// 判断想要删除的数据是否存在
		TrainSeat ts = trainSeatMapper.selectById(trainSeatDeleteDTO.getId());
		if(ObjectUtil.isNull(ts)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		trainSeatMapper.deleteById(trainSeatDeleteDTO.getId());
	}

	@Override
	public void update(TrainSeatUpdateDTO trainSeatUpdateDTO){
		// 判断想要修改的数据是否存在
		TrainSeat tc = trainSeatMapper.selectById(trainSeatUpdateDTO.getId());
		if(ObjectUtil.isNull(tc)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DATA_DOES_NOT_EXIST);
		}

		// 修改座位
		TrainSeat trainSeat = BeanUtil.copyProperties(trainSeatUpdateDTO, TrainSeat.class);
		trainSeat.setUpdateTime(new Date());
		trainSeatMapper.updateById(trainSeat);
	}

	@Override
	public void insert(TrainSeatInsertDTO trainSeatInsertDTO){
		// 新增座位
		TrainSeat trainSeat = BeanUtil.copyProperties(trainSeatInsertDTO, TrainSeat.class);
		trainSeat.setId(SnowUtil.getSnowflakeNextId());
		trainSeat.setCreateTime(new Date());
		trainSeat.setUpdateTime(new Date());
		trainSeatMapper.insert(trainSeat);
	}

	@Override
	public void generatorSeat(String trainCode){
		// 判断想要修改的数据是否存在
		if(StrUtil.isBlank(trainCode)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}
		QueryWrapper<TrainSeat> trainSeatQueryWrapper = new QueryWrapper<>();
		trainSeatQueryWrapper.eq("train_code", trainCode);
		List<TrainSeat> trainSeatList = trainSeatMapper.selectList(trainSeatQueryWrapper);
		if(ObjectUtil.isNull(trainSeatList)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		// 根据车次编号删除座位
		QueryWrapper<TrainSeat> trainSeatDeleteWrapper = new QueryWrapper<>();
		trainSeatDeleteWrapper.eq("train_code", trainCode);
		trainSeatMapper.delete(trainSeatDeleteWrapper);

		// 根据车次编号查询所有的车厢
		QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
		trainCarriageQueryWrapper.orderByAsc("`index`");
		trainCarriageQueryWrapper.eq("train_code", trainCode);
		List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectList(trainCarriageQueryWrapper);

		// 循环生成每个车厢的座位
		for(TrainCarriage trainCarriage : trainCarriageList){
			// 获取行数，座位类型
			Integer rowCount = trainCarriage.getRowCount();
			String seatType = trainCarriage.getSeatType();
			// 座序
			int carriageSeatIndex = 1;

			// 根据车厢座位类型，筛选出列
			List<SeatColEnum> seatColEnumList = SeatColEnum.getColsByType(seatType);
			// 循环行
			for(int row = 1; row <= rowCount; row++){
				// 循环列
				for(SeatColEnum seatColEnum : seatColEnumList){
					// 新增座位
					TrainSeat trainSeat = new TrainSeat();
					trainSeat.setId(SnowUtil.getSnowflakeNextId());
					trainSeat.setTrainCode(trainCode);
					trainSeat.setCarriageIndex(trainCarriage.getIndex());
					trainSeat.setRow(StrUtil.fillBefore(String.valueOf(row), '0', 2));
					trainSeat.setCol(seatColEnum.getCode());
					trainSeat.setSeatType(seatType);
					trainSeat.setCarriageSeatIndex(carriageSeatIndex++);
					trainSeat.setCreateTime(new Date());
					trainSeat.setUpdateTime(new Date());
					trainSeatMapper.insert(trainSeat);
				}
			}
		}
	}
}
